package love.yuanmengrobot.guoyitang.service.impl;

/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:lzl
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/8/11 15:44
 */


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import love.yuanmengrobot.guoyitang.entity.TbNatmehaChineseMedicine;
import love.yuanmengrobot.guoyitang.entity.TbNatmehaHotspot;
import love.yuanmengrobot.guoyitang.mapper.TbNatmehaChineseMedicineMapper;
import love.yuanmengrobot.guoyitang.mapper.TbNatmehaHotspotMapper;
import love.yuanmengrobot.guoyitang.service.HotSpotsService;
import love.yuanmengrobot.guoyitang.vo.Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class HotSpotsServiceImpl implements HotSpotsService {
    @Autowired
    private TbNatmehaHotspotMapper hotspotMapper;

    @Autowired
    private TbNatmehaChineseMedicineMapper chineseMedicineMapper;

    @Override
    public Vo queryByLimit(Integer current, Integer size , String dataType) {
        //默认不传参数时
        if(current==null) current=1;
        if(size==null) size=5;

        if("zycs".equals(dataType)){
            //中药常识
            return queryChineseMedicine(current,size);
        }else{
            IPage<TbNatmehaHotspot> hotspotIPage=new Page<>(current,size);
            hotspotIPage=hotspotMapper.selectPage(hotspotIPage,new LambdaQueryWrapper<TbNatmehaHotspot>()
                    .eq(dataType!=null, TbNatmehaHotspot::getDataType,dataType)
            );
            Vo<TbNatmehaHotspot> voHotspot=new Vo<>();
            voHotspot.setCurrent(current);
            voHotspot.setSize(size);
            voHotspot.setTotal(hotspotIPage.getTotal());
            voHotspot.setList(hotspotIPage.getRecords());
            return voHotspot;
        }

    }

    @Override
    public TbNatmehaHotspot queryByIdAndCode(String id, String itemCode) {
        return hotspotMapper.selectOne(new QueryWrapper<TbNatmehaHotspot>().eq("itemID",id).eq("itemCode",itemCode));
    }

    public Vo<TbNatmehaChineseMedicine> queryChineseMedicine(Integer current, Integer size){
        //分页查询
        IPage<TbNatmehaChineseMedicine> iPage=new Page<>(current,size);
        iPage=chineseMedicineMapper.selectPage(iPage,null);
        Vo<TbNatmehaChineseMedicine> voChineseMedicine=new Vo<>();
        voChineseMedicine.setCurrent(current);
        voChineseMedicine.setSize(size);
        voChineseMedicine.setTotal(iPage.getTotal());
        voChineseMedicine.setList(iPage.getRecords());

        return voChineseMedicine;
    }
}
